package com.atguigu.eduorder.service.impl;

import com.atguigu.commonutils.vo.CourseWebOrderVO;
import com.atguigu.commonutils.vo.UcenterMemberVO;
import com.atguigu.eduorder.client.EduClient;
import com.atguigu.eduorder.client.UcenterClient;
import com.atguigu.eduorder.entity.Order;
import com.atguigu.eduorder.mapper.OrderMapper;
import com.atguigu.eduorder.service.OrderService;
import com.atguigu.eduorder.utils.OrderNoUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author zr
 * @since 2021-11-25
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    /**
     * 注入远程调用的接口
     */
    @Resource
    private EduClient eduClient;
    @Resource
    private UcenterClient ucenterClient;

    @Override
    public String saveOrder(String courseId, String memberId) {
        // 通过远程调用根据courseId获取课程详细信息
        CourseWebOrderVO courseInfoOrder = eduClient.getCourseInfoOrder(courseId);
        // 通过远程调用根据memberId获取用户详细信息
        UcenterMemberVO memberInfo = ucenterClient.getMemberInfoById(memberId);
        // 创建Order对象 向里面设置所需要的数据
        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());
        order.setCourseId(courseId);
        order.setCourseTitle(courseInfoOrder.getTitle());
        order.setCourseCover(courseInfoOrder.getCover());
        order.setTeacherName(courseInfoOrder.getTeacherName());
        order.setTotalFee(courseInfoOrder.getPrice());
        order.setMemberId(memberId);
        order.setMobile(memberInfo.getMobile());
        order.setNickname(memberInfo.getNickname());
        // 订单状态 0表示未支付 1表示已支付
        order.setStatus(0);
        // 支付类型  1表示微信 0表示支付宝
        order.setPayType(1);
        baseMapper.insert(order);
        return order.getOrderNo();
    }
}
